﻿using System;
using System.Collections.Generic;
using System.Text;
using Vortex;
using Vortex.Drawing;

namespace Vortex.Tutorials.TexturesAndSprites {
    class TexturesAndSprites : Game {
        Texture _SpaceTexture;
        Texture _SpaceshipTexture;
        Texture _SpaceStationTexture;

        Sprite _SpaceShip;
        Sprite _SpaceStation;

        Vector2 _SpaceShipPosition;

        protected override void Load () {
            //textures loaded from JPEG files automaticaly created with PixelFormat.Default (PixelFormat.X8R8G8B8)
            _SpaceTexture = new Texture(@"{graphics}\space.jpg");

            //PNG images could be loaded into texture with alpha channel or without it depending of file content
			_SpaceshipTexture = new Texture(@"{graphics}\spaceship.png");

            //space station will have less quality than other textures. Its pixel format is override down to 16bit
			_SpaceStationTexture = new Texture(@"{graphics}\spacestation.png", PixelFormat.A4R4G4B4);

            _SpaceShip = new Sprite(_SpaceshipTexture);
			//creates sprite with explicit coordinates and moves origin point of sprite in texture coordinate space
            _SpaceStation = _SpaceStationTexture.ToSprite(0, 0, 256, 256).MoveOriginTo(128, 80); //or just _SpaceStationTexture.ToSprite();
        }

        protected override void Unload () {
            _SpaceTexture.Dispose();
            _SpaceshipTexture.Dispose();
        }

        protected override void Update (GameTime time) {
            _SpaceShipPosition = new Vector2(700 - (time.TotalGameTime * 10) % 800, 400);
        }

        protected override void Render (Canvas2D canvas) {
            //no need to clear canvas before drawing. Space background will overwrite previous frame data

            //sprite is stretching space background to whole screen, Texture.ToSprite() return sprite targeted whole texture surface
            // canvas.Region is equivalent to new Rect(0, 0, canvas.Width, canvas.Height)
            canvas.DrawSprite(canvas.Region, _SpaceTexture.ToSprite(), ColorU.White);
			

            //drawing spaceship targeting its center point and sprite size
            canvas.DrawSprite(_SpaceShipPosition, new Vector2(128, 64), _SpaceShip, ColorU.White);
			//draw origin point
			canvas.DrawFilledEllipse(Rect.FromPoint(_SpaceShipPosition, new Vector2(5)), ColorU.Red.MultiplyAlpha(0.75f));
			

            //sprite angular rotation around center is used for drawing space-station 
			canvas.DrawSprite(450, 170, Game.Time.TotalGameTime * 0.1f, _SpaceStation, ColorU.White);
			//draw origin point
			canvas.DrawFilledEllipse(Rect.FromPoint(450, 170, 5, 5), ColorU.Red.MultiplyAlpha(0.75f));

        }
    }
}
